// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   moduleBalance_draft_ok.java

package draft.manufacture;

import include.nseer_cookie.*;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.ValidataNumber;
import validata.ValidataRecord;

public class moduleBalance_draft_ok extends HttpServlet
{

	public moduleBalance_draft_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		javax.servlet.ServletContext dbApplication = dbSession.getServletContext();
		HttpSession session = request.getSession();
		nseer_db_backup1 manufacture_db = new nseer_db_backup1(dbApplication);
		nseer_db_backup1 stock_db = new nseer_db_backup1(dbApplication);
		ValidataNumber validata = new ValidataNumber();
		counter count = new counter(dbApplication);
		ValidataRecord vr = new ValidataRecord();
		String time = "";
		Date now = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		time = formatter.format(now);
		try
		{
			if (manufacture_db.conn((String)dbSession.getAttribute("unit_db_name")) && stock_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				String checker_ID = request.getParameter("checker_ID");
				String config_id = request.getParameter("config_id");
				String manufacture_ID = request.getParameter("manufacture_ID");
				String module_time = request.getParameter("module_time");
				String procedure_ID = request.getParameter("procedure_ID");
				String procedure_name = request.getParameter("procedure_name");
				String register_time = request.getParameter("register_time");
				String procedure_responsible_person = request.getParameter("procedure_responsible_person");
				String checker = request.getParameter("checker");
				String product_IDn[] = request.getParameterValues("product_ID");
				String bodyc = new String(request.getParameter("reason").getBytes("UTF-8"), "UTF-8");
				String reason = exchange.toHtml(bodyc);
				String product_amount = request.getParameter("product_amount");
				String product_namen[] = request.getParameterValues("product_name");
				String product_describen[] = request.getParameterValues("product_describe");
				String cost_pricen[] = request.getParameterValues("cost_price");
				String amountn[] = request.getParameterValues("amount");
				String amount_unitn[] = request.getParameterValues("amount_unit");
				int num = Integer.parseInt(product_amount);
				int p = 0;
				int m = 0;
				String product_ID_group = ",";
				for (int i = 1; i <= num; i++)
				{
					String tem_amount = (new StringBuilder("amount")).append(i).toString();
					String amount = request.getParameter(tem_amount);
					if (!validata.validata(amount))
						p++;
				}

				for (int j = 1; j < product_namen.length; j++)
				{
					product_ID_group = (new StringBuilder(String.valueOf(product_ID_group))).append(product_IDn[j]).append(",").toString();
					if (!validata.validata(amountn[j]))
						p++;
				}

				for (int i = 1; i <= num; i++)
				{
					String tem_product_ID = (new StringBuilder("product_ID")).append(i).toString();
					String product_ID = request.getParameter(tem_product_ID);
					if (product_ID_group.indexOf(product_ID) != -1)
						m++;
				}

				String sql88 = (new StringBuilder("select * from manufacture_module_balance where manufacture_ID='")).append(manufacture_ID).append("' and module_time='").append(module_time).append("' and procedure_name='").append(procedure_name).append("' and (check_tag='9' or check_tag='5')").toString();
				ResultSet rs88 = manufacture_db.executeQuery(sql88);
				if (rs88.next())
				{
					if (p == 0)
					{
						if (m == 0)
						{
							String pay_ID = NseerId.getId("stock/pay", (String)dbSession.getAttribute("unit_db_name"));
							try
							{
								double cost_price_sum = 0.0D;
								double demand_amount = 0.0D;
								for (int i = 1; i <= num; i++)
								{
									String tem_product_name = (new StringBuilder("product_name")).append(i).toString();
									String tem_product_ID = (new StringBuilder("product_ID")).append(i).toString();
									String tem_product_describe = (new StringBuilder("product_describe")).append(i).toString();
									String tem_amount = (new StringBuilder("amount")).append(i).toString();
									String tem_amount_unit = (new StringBuilder("amount_unit")).append(i).toString();
									String tem_cost_price = (new StringBuilder("cost_price")).append(i).toString();
									String product_name = request.getParameter(tem_product_name);
									String product_ID = request.getParameter(tem_product_ID);
									String product_describe = request.getParameter(tem_product_describe);
									String amount = request.getParameter(tem_amount);
									String amount_unit = request.getParameter(tem_amount_unit);
									String cost_price2 = request.getParameter(tem_cost_price);
									StringTokenizer tokenTO3 = new StringTokenizer(cost_price2, ",");
									String cost_price;
									String cost_price1;
									for (cost_price = ""; tokenTO3.hasMoreTokens(); cost_price = (new StringBuilder(String.valueOf(cost_price))).append(cost_price1).toString())
										cost_price1 = tokenTO3.nextToken();

									double subtotal = Double.parseDouble(cost_price) * Double.parseDouble(amount);
									cost_price_sum += subtotal;
									demand_amount += Double.parseDouble(amount);
									String sql2 = (new StringBuilder("update manufacture_module_balance_details set amount='")).append(amount).append("',subtotal='").append(subtotal).append("' where manufacture_ID='").append(manufacture_ID).append("' and procedure_name='").append(procedure_name).append("' and register_time='").append(register_time).append("' and details_number='").append(i).append("'").toString();
									manufacture_db.executeUpdate(sql2);
								}

								for (int j = 1; j < product_namen.length; j++)
								{
									StringTokenizer tokenTO2 = new StringTokenizer(cost_pricen[j], ",");
									String cost_price;
									String cost_price1;
									for (cost_price = ""; tokenTO2.hasMoreTokens(); cost_price = (new StringBuilder(String.valueOf(cost_price))).append(cost_price1).toString())
										cost_price1 = tokenTO2.nextToken();

									double subtotal = Double.parseDouble(cost_price) * Double.parseDouble(amountn[j]);
									cost_price_sum += subtotal;
									int details_number = num + j;
									String sql4 = (new StringBuilder("insert into manufacture_module_balance_details(manufacture_ID,procedure_name,details_number,product_ID,product_name,product_describe,amount,cost_price,amount_unit,subtotal,register_time,module_time) values ('")).append(manufacture_ID).append("','").append(procedure_name).append("','").append(details_number).append("','").append(product_IDn[j]).append("','").append(product_namen[j]).append("','").append(product_describen[j]).append("','").append(amountn[j]).append("','").append(cost_price).append("','").append(amount_unitn[j]).append("','").append(subtotal).append("','").append(register_time).append("','").append(module_time).append("')").toString();
									manufacture_db.executeUpdate(sql4);
								}

								String sql3 = (new StringBuilder("update manufacture_module_balance set module_cost_price_sum='")).append(cost_price_sum).append("',procedure_responsible_person='").append(procedure_responsible_person).append("',checker='").append(checker).append("',reason='").append(reason).append("' where manufacture_ID='").append(manufacture_ID).append("' and module_time='").append(module_time).append("' and procedure_name='").append(procedure_name).append("' and module_time='").append(module_time).append("'").toString();
								manufacture_db.executeUpdate(sql3);
							}
							catch (Exception ex)
							{
								ex.printStackTrace();
							}
							response.sendRedirect("draft/manufacture/moduleBalance_ok.jsp?finished_tag=0");
						} else
						{
							response.sendRedirect("draft/manufacture/moduleBalance_ok.jsp?finished_tag=6");
						}
					} else
					{
						response.sendRedirect("draft/manufacture/moduleBalance_ok.jsp?finished_tag=5");
					}
				} else
				{
					response.sendRedirect("draft/manufacture/moduleBalance_ok.jsp?finished_tag=1");
				}
				manufacture_db.commit();
				stock_db.commit();
				stock_db.close();
				manufacture_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
